package com.hxci.pointmap.service;

import com.hxci.pointmap.model.EnvironmentData;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Calendar;

public class DataGeneratorService {
    private static final String[] DEVICE_NAMES = {
        "灌溉系统-1", "土壤监测仪-A", "气象站-南", "病虫害监测仪-1",
        "灌溉系统-2", "土壤监测仪-B", "气象站-北", "病虫害监测仪-2",
        "灌溉系统-3", "土壤监测仪-C", "气象站-东", "病虫害监测仪-3"
    };

    private Random random = new Random();
    
    // 固定24小时环境数据
    private static final double[] FIXED_24H_TEMPERATURES = {20.5, 21.2, 22.0, 23.5, 25.0, 26.8, 28.5, 30.0, 31.2, 32.5, 33.0, 32.5, 31.0, 30.0, 28.5, 27.0, 26.0, 25.5, 24.0, 23.0, 22.0, 21.5, 20.8, 20.0};
    private static final double[] FIXED_24H_HUMIDITIES = {75.0, 73.5, 72.0, 70.0, 68.0, 65.5, 62.0, 60.0, 58.5, 57.0, 56.0, 57.5, 60.0, 63.0, 66.0, 69.0, 71.5, 73.0, 74.5, 75.5, 76.0, 75.5, 75.0, 74.5};
    private static final double[] FIXED_24H_LIGHTS = {0, 0, 100, 500, 1500, 3000, 5000, 7000, 8500, 9500, 10000, 9500, 8000, 6000, 4000, 2000, 800, 200, 50, 0, 0, 0, 0, 0};
    private static final double[] FIXED_24H_CO2S = {400, 405, 410, 415, 420, 425, 430, 435, 440, 445, 450, 445, 440, 435, 430, 425, 420, 415, 410, 405, 400, 395, 390, 385};
    
    // 固定7天环境数据
    private static final double[] FIXED_7D_TEMPERATURES = {22.0, 23.5, 24.0, 25.5, 26.0, 25.0, 23.0};
    private static final double[] FIXED_7D_HUMIDITIES = {65.0, 63.0, 60.0, 58.0, 62.0, 68.0, 70.0};
    private static final double[] FIXED_7D_LIGHTS = {7000, 7500, 8000, 8500, 8200, 7800, 7200};
    private static final double[] FIXED_7D_CO2S = {420, 430, 440, 450, 445, 435, 425};
    
    // 缓存数据和生成时间
    private static List<EnvironmentData> cached7DaysData = null;
    private static List<EnvironmentData> cached24HoursData = null;
    private static long last7DaysDataGenerationTime = 0;
    private static long last24HoursDataGenerationTime = 0;

    // 7天的毫秒数
    private static final long SEVEN_DAYS_IN_MILLIS = 7 * 24 * 60 * 60 * 1000;

    /**
     * 生成指定数量的随机环境数据
     * @param count 要生成的数据数量
     * @return 环境数据列表
     */
    public List<EnvironmentData> generateEnvironmentData(int count) {
        List<EnvironmentData> dataList = new ArrayList<>();
        long currentTime = System.currentTimeMillis();

        for (int i = 0; i < count; i++) {
            // 随机选择设备
            String deviceName = DEVICE_NAMES[random.nextInt(DEVICE_NAMES.length)];

            // 生成随机数据
            double temperature = 15 + random.nextDouble() * 20;  // 15-35℃
            double humidity = 40 + random.nextDouble() * 40;     // 40-80%
            double light = 1000 + random.nextDouble() * 9000;   // 1000-10000 lux
            double co2 = 300 + random.nextDouble() * 700;       // 300-1000 ppm

            // 时间递减，模拟历史数据
            Date recordTime = new Date(currentTime - (count - i) * 60000); // 每条数据间隔1分钟

            EnvironmentData data = new EnvironmentData(
                i + 1, 
                deviceName, 
                Math.round(temperature * 10) / 10.0, 
                Math.round(humidity * 10) / 10.0,
                Math.round(light),
                Math.round(co2 * 10) / 10.0,
                recordTime
            );

            dataList.add(data);
        }

        return dataList;
    }

    /**
     * 生成最近24小时的环境数据，每小时一个点
     * @return 环境数据列表
     */
    public List<EnvironmentData> generateLast24HoursData() {
        // 如果已有缓存数据，直接返回
        if (cached24HoursData != null) {
            return cached24HoursData;
        }
        
        List<EnvironmentData> dataList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        // 设置到整点
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        // 生成24小时数据，从当前时间往前推
        for (int i = 23; i >= 0; i--) {
            // 每一次循环，时间往前推一小时
            Calendar timeCalendar = (Calendar) calendar.clone();
            timeCalendar.add(Calendar.HOUR_OF_DAY, -i);
            Date recordTime = timeCalendar.getTime();
            
            // 使用固定设备
            String deviceName = DEVICE_NAMES[0];

            // 使用预设数据
            int dataIndex = 23 - i;
            double temperature = FIXED_24H_TEMPERATURES[dataIndex];
            double humidity = FIXED_24H_HUMIDITIES[dataIndex];
            double light = FIXED_24H_LIGHTS[dataIndex];
            double co2 = FIXED_24H_CO2S[dataIndex];

            EnvironmentData data = new EnvironmentData(
                24 - i, 
                deviceName, 
                Math.round(temperature * 10) / 10.0, 
                Math.round(humidity * 10) / 10.0,
                Math.round(light),
                Math.round(co2 * 10) / 10.0,
                recordTime
            );

            dataList.add(data);
        }

        return dataList;
    }

    /**
     * 生成最近7天的环境数据，每天一个点
     * @return 环境数据列表
     */
    public List<EnvironmentData> generateLast7DaysData() {
        List<EnvironmentData> dataList = new ArrayList<>();
        long currentTime = System.currentTimeMillis();

        for (int i = 6; i >= 0; i--) {
            // 随机选择设备
            String deviceName = DEVICE_NAMES[random.nextInt(DEVICE_NAMES.length)];

            //
            double temperature = 15 + random.nextDouble() * 20;  // 15-35℃
            double humidity = 40 + random.nextDouble() * 40;     // 40-80%
            double light = 1000 + random.nextDouble() * 9000;   // 1000-10000 lux
            double co2 = 300 + random.nextDouble() * 700;       // 300-1000 ppm

            // 时间递减，历史数据
            Date recordTime = new Date(currentTime - i * 24 * 60 * 60 * 1000); // 每天一个数据点

            EnvironmentData data = new EnvironmentData(
                7 - i, 
                deviceName, 
                Math.round(temperature * 10) / 10.0, 
                Math.round(humidity * 10) / 10.0,
                Math.round(light),
                Math.round(co2 * 10) / 10.0,
                recordTime
            );

            dataList.add(data);
        }

        return dataList;
    }
}
